<?php
/**
 * Created by PhpStorm.
 * User: fengdh
 * Date: 2019/8/9
 * Time: 17:18
 */

namespace search\controllers\platform;

use business\elasticsearch\services\ProcurementRecommendService;
use business\elasticsearch\services\ProcurementService;
use search\models\ProcurementSearchForm;
use yii\web\Controller;

class ProcurementController extends BaseController
{
    /**
     * 平台招标招募搜索—（此接口仅提供给寻源—招标招募调用，其它业务禁止调用）
     */
    public function actionIndex()
    {
        $searchForm = new ProcurementSearchForm();
        $searchForm->attributes = array_map('common\utils\StringUtils::utf8_clean', $_GET);
        $resultSet = $searchForm->search();

        return $resultSet;
    }

    /*
     * 平台招标招募列表—（此接口仅提供给非寻源业务调用）
     * */
    public function actionList()
    {
        try {
            $categoryCode = \Yii::$app->request->get("category_code", []);
            $page = \Yii::$app->request->get("page", 1);
            $pageSize = \Yii::$app->request->get("page_size", 10);
            //是否独家招标招募信息推荐,最新的3条服务品类匹配的招募+展示最新3条服务品类匹配的招标,按发布时间逆序
            $is_exclusive = \Yii::$app->request->get("is_exclusive", 0);
            if (!is_array($categoryCode)) {
                throw new \Exception("categoryCode格式不正确");
            }
            foreach ($categoryCode as $item) {
                if (!is_numeric($item)) {
                    throw new \Exception("categoryCode格式不正确");
                }
            }

            $searchForm = new ProcurementSearchForm();
            if ($is_exclusive) {
                $resultSet = $searchForm->search2($categoryCode, $page, ceil($pageSize/2), 'recruit', false);
                $result = $searchForm->search2($categoryCode, $page, $pageSize-ceil($pageSize/2), 'bidding', false);
                $resultSet = array_merge($resultSet, $result);
                usort($resultSet, function($a, $b){
                    if(strtotime($a['publish_date'])==strtotime($b['publish_date'])){
                        return 0;
                    }
                    return strtotime($a['publish_date'])<strtotime($b['publish_date'])?1:-1;
                });
            }else{
                $resultSet = $searchForm->search2($categoryCode, $page, $pageSize);
            }
            return $this->success($resultSet);
        } catch (\Exception $e) {
            $code = $e->getCode();
            $message = !empty($code) ? $e->getMessage() : '系统异常，请稍后重试';
            \Yii::error('errorRequest:' . __METHOD__ . ':' . $e);
            return $this->error($code, $message);
        }

    }

    public function actionGetCount()
    {
        try {
            $categoryCode = \Yii::$app->request->get('categoryCode', '');
            $data = (new ProcurementService())->getCount($categoryCode);
            return ['success' => true, 'errorCode' => null, 'message' => null, 'data' => $data];
        } catch (\Exception $ex){
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }

    /**
     * 招募/招标推荐
     * @return array
     */
    public function actionRecommend()
    {
        try {
            $type = \Yii::$app->request->get('type', 'all');
            $pageIndex = \Yii::$app->request->get('pageIndex', 1);
            $pageSize = \Yii::$app->request->get('pageSize', 10);
            if (!isset($pageIndex)) {
                $pageIndex = 1;
            }
            if (!isset($pageSize)) {
                $pageSize = 10;
            }
            $data = (new ProcurementRecommendService())->getList($type, $pageIndex, $pageSize);
            return ['success' => true, 'errorCode' => null, 'message' => null, 'data' => $data];
        } catch (\Exception $ex){
            \Yii::error($ex->getMessage());
            return ['success' => false, 'errorCode' => $ex->getCode(), 'message' => $ex->getMessage(), 'data' => null];
        }
    }
}
